﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SMS.DAL.Providers
{
    public class ItemTransactionProvider : ProviderBase<SMS.DAL.ItemsTransaction, ItemTransactionSearchCriteria>
    {
        public override void Insert(ItemsTransaction item)
        {
            Context.ItemsTransactions.AddObject(item);
            Context.SaveChanges();
        }

        public override void Update(ItemsTransaction updatedItem)
        {
            throw new NotImplementedException();
        }

        public override void Delete(int id)
        {
            Context.ItemsTransactions.DeleteObject(this.GetByID(id));
            Context.SaveChanges();
        }

        public override ItemsTransaction GetByID(int id)
        {
            return (from c in Context.ItemsTransactions
                    where c.ID == id
                    select c).SingleOrDefault();
        }

        public override List<ItemsTransaction> Search(ItemTransactionSearchCriteria SearchCriteria)
        {
            return (from c in Context.ItemsTransactions
                    where (SearchCriteria.Barcode == String.Empty || c.Item.BarCode == SearchCriteria.Barcode) &&
                          (SearchCriteria.StockID == null || c.StockID == SearchCriteria.StockID) &&
                          (SearchCriteria.StartDate == null || c.Date >= SearchCriteria.StartDate) &&
                          (SearchCriteria.EndDate == null || c.Date <= SearchCriteria.EndDate)

                    select c).ToList();
        }
    }
}
